const category = require('../models/category')

// 获取商品类别
const getCategorys = async (ctx) => {
  const query = ctx.request.query
  const data = await category.getCategorys(query)
  ctx.body = {
    code: 0,
    message: '成功',
    data
  }
}
// 获取商品类别
const getCategory = async (ctx) => {
  const data = await category.getCategory()
  ctx.body = {
    code: 0,
    message: '成功',
    data
  }
}
// 添加商品类别
const addCategory = async (ctx) => {
  const data = ctx.request.body
  const result = await category.addCategory(data)
  if(result) {
    ctx.body = {
      code: 0,
      message: '成功'
    }
  }else {
    ctx.body = {
      code: -1,
      message: '失败'
    }
  }
}
// 根据id获取商品类别
const getCategoryById = async (ctx) => {
  const id = ctx.params.id
  const data = await category.getCategoryById(id)
  ctx.body = {
    code: 0,
    message: '成功',
    data
  }
}
// 根据id修改商品类别
const updateCategoryById = async (ctx) => {
  const id = ctx.params.id
  const data = ctx.request.body
  const result = await category.updateCategoryById(id, data)
  if(result) {
    ctx.body = {
      code: 0,
      message: '成功'
    }
  }else {
    ctx.body = {
      code: -1,
      message: '失败'
    } 
  }
}
// 根据关键字获取商品类别
const getCategoryByKeyWords = async (ctx) => {
  const keywords = ctx.request.query
  const data = await category.getCategoryByKeyWords(keywords)
  if(data) {
    ctx.body = {
      code: 0,
      message: '成功',
      data
    }
  }else {
    ctx.body = {
      code: -1,
      message: '失败'
    }
  }
}
// 删除商品类别
const deleteCategory = async (ctx) => {
  const id = ctx.params.id
  const result = await category.deleteCategory(id)
  if(result) {
    ctx.body = {
      code: 0,
      message: '成功'
    }
  }else {
    ctx.body = {
      code: -1,
      message: '失败'
    }
  }
}
module.exports = {
  getCategorys,
  getCategory,
  addCategory,
  getCategoryById,
  updateCategoryById,
  getCategoryByKeyWords,
  deleteCategory
}